package youleche.com.controler.servlet;

import java.beans.IntrospectionException;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.util.LinkedList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;

import youleche.com.business.BFixInfo;
import youleche.com.business.BUser;
import youleche.com.common.ParamConfigParameters;
import youleche.com.controler.servlet.common.UlecheHttpServlet;
import youleche.com.model.MFixInfo;
import youleche.com.model.MUser;
import youleche.com.util.AESEncryptAndDecrypt;
import youleche.com.util.DateUtil;
import youleche.com.util.JsonHelper;

/**
 * Servlet修理信息serblet
 */
public class SFixInfoServletAuthority extends UlecheHttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SFixInfoServletAuthority() {
        super();
        // TODO Auto-generated constructor stub
    }
    
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int action = Integer.parseInt(request.getParameter("action"));
		switch(action){
			case 0:
				//根据ID得到用户修理信息列表总数
				this.getFixInfoCountBySellerId(request, response);
				break;
			case 1:
				//根据ID得到用户修理信息列表
				this.getFixInfosBySellerId(request, response);
				break;
			case 2: 
				//置顶修理
				this.topFixInfoById(request, response);
				break;
		}
	}
	
	/**
	 * 置顶修理
	 * @param request
	 * @param response
	 */
	private void topFixInfoById(HttpServletRequest request, HttpServletResponse response){
		try{
			String fixId = request.getParameter("fixId");
			BFixInfo handler = new BFixInfo();
			super.response(response, ""+handler.topFixInfoById(fixId));
		}catch(Exception e){
			e.printStackTrace();
			super.response(response, "0");
		}
	}
	
	/**
	 * 根据ID得到用户修理信息列表
	 * @param request
	 * @param response
	 */
	private void getFixInfosBySellerId(HttpServletRequest request, HttpServletResponse response){
		try{
			int pageSize = Integer.parseInt(request.getParameter("pageSize"));
			Integer startIndex = (Integer.parseInt(request.getParameter("pageIndex")) - 1) * pageSize ;
			String userName = request.getParameter("userName");
			MUser user = new BUser().getUserByUserName(userName);
			if(user == null){
				super.response(response, "");
				return;
			}
			ParamConfigParameters paramConfig = new ParamConfigParameters();
			String key = paramConfig.getParameter("LinkNum_KEY");
			BFixInfo handler = new BFixInfo();
			List<MFixInfo> list = handler.getFixInfosBySellerId(startIndex.toString(), ((Integer)pageSize).toString(),user.getId());
			List<MFixInfo> resList = new LinkedList<MFixInfo>();
			if(list == null || list.size() == 0){
				super.response(response, "");
				return;
			}
			for (MFixInfo fix : list) {
				fix.setPhoneNum(AESEncryptAndDecrypt.decrypt(fix.getPhoneNum(), key));
				resList.add(fix);
			}
			
			JsonHelper.printListToJsonString(response,resList);
		}catch(Exception e){
			e.printStackTrace();
			super.response(response, "");
		}
	}
	
	/**
	 * 根据ID得到用户修理信息列表总数
	 * @param request
	 * @param response
	 */
	private void getFixInfoCountBySellerId(HttpServletRequest request, HttpServletResponse response){
		try {
			String userName = request.getParameter("userName");
			MUser user = new BUser().getUserByUserName(userName);
			if(user == null){
				super.response(response, "0");
				return;
			}
			
			BFixInfo handler = new BFixInfo();
			super.response(response, handler.getFixInfoCountBySellerId(user.getId()));
		} catch (Exception e) {
			e.printStackTrace();
			super.response(response, "0");
		} 
	}
	
	

}
